#include "box.h"

Box::Box(int n, int e, int s, int w){
    this->_coord[NORTH] = n;
    this->_coord[EAST] = e;
    this->_coord[SOUTH] = s;
    this->_coord[WEST] = w;

    _onTarget = false;

    /* Montar imagem deste bloco com as coordenadas. */
    _img = al_create_bitmap(BLOCK_W, BLOCK_H);
    mount_img();
}

void Box::mount_img(){
    ALLEGRO_BITMAP* lastBmp = al_get_target_bitmap();
    al_set_target_bitmap(_img);

    if(_onTarget){
        al_draw_bitmap(Resources::instance()->image( Resources::BOX_OK ),0,0,0);

    } else {
        al_draw_bitmap(Resources::instance()->image( Resources::BOX ),0,0,0);
    }

    al_draw_bitmap(Resources::instance()->image( getPoleImg(_coord[NORTH])  ), BLOCK_W / 2 - 5, 0, 0);
    al_draw_bitmap(Resources::instance()->image( getPoleImg(_coord[EAST]) ), BLOCK_W - 10, BLOCK_H / 2 - 5,0);
    al_draw_bitmap(Resources::instance()->image( getPoleImg(_coord[SOUTH]) ), BLOCK_W / 2  - 5, BLOCK_H - 10,0);
    al_draw_bitmap(Resources::instance()->image( getPoleImg(_coord[WEST]) ), 0, BLOCK_H / 2  - 5,0);

    al_set_target_bitmap(lastBmp);
}

Resources::e_Images Box::getPoleImg(int v){
    if(v == Resources::TYPE_BLOCK_NONE) return Resources::P_NOTHING;
    if(v == Resources::TYPE_BLOCK_NORTH) return Resources::P_NORTH;
    if(v == Resources::TYPE_BLOCK_SOUTH) return Resources::P_SOUTH;
    if(v == Resources::TYPE_BLOCK_IRON) return Resources::P_IRON;
    return Resources::P_NOTHING;
}

ALLEGRO_BITMAP* Box::render(){
    return _img;
}

Box::~Box(){
    al_destroy_bitmap(_img);
    delete[] _coord;
}

std::string Box::saveString(){
    std::string s;
    s+= (char) Resources::TYPE_TILE_BLOCK;
    s+=(char)_coord[NORTH];
    s+=(char)_coord[EAST];
    s+=(char)_coord[SOUTH];
    s+=(char)_coord[WEST];
    return s;
}

Block* Box::copy(){
    return new Box(_coord[NORTH],_coord[EAST],_coord[SOUTH],_coord[WEST]);
}
